package com.tgy.nowcoder;

import java.util.HashMap;
import java.util.Map;

public class NC41_最长无重复子串_01 {

    /**
     *
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public static int maxLength (int[] arr) {
        // write code here

        if (arr.length == 1) {

            return 1;
        }

        int res = 0;

        Map<Integer,Integer> map = new HashMap<>();

        int left = 0,right = 0;

        while (right < arr.length) {

            int val = arr[right];
            if (map.containsKey(val)) {

                left = Math.max(left,map.get(val)+1);
            }
            res = Math.max(res,right - left + 1);
            map.put(val,right);

            right++;
        }

        return res;
    }

    public static void main(String[] args) {

        int[] nums = {2,2,1};
        System.out.println(maxLength(nums));
    }
}
